package main

import (
	"database/sql"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
)

const DB_NAME = "mysql"
const DB_SOURCE = "root:123456@tcp(127.0.0.1:3306)/t1"

var db *sql.DB

func init(){
	var err error
	db,err = sql.Open(DB_NAME,DB_SOURCE)
	if err != nil{
		fmt.Println(err)
	}



}

func main() {
	defer db.Close()
	//insert()
	//query()
	dropTable()
}

func dropTable(){
	result,err := db.Exec("DROP TABLE IF EXISTS `jy`")
	fmt.Println(result)
	fmt.Println(err)

	result,err = db.Exec(`CREATE TABLE jy (
  id int(11) NOT NULL AUTO_INCREMENT,
  nick_name varchar(50) DEFAULT NULL,
  age int(3) DEFAULT NULL,
  marriage varchar(20) DEFAULT NULL,
  height varchar(10) DEFAULT NULL,
  education varchar(10) DEFAULT NULL,
  work_location varchar(50) DEFAULT NULL,
  short_note varchar(200) DEFAULT NULL,
  match_condition varchar(200) DEFAULT NULL,
  PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8`)
}

func query(){

	var(
		id int
		nickName string
		age int
	)

	rows,err := db.Query("select id,nick_name,age from jy")
	if err != nil{
		fmt.Println(err)
		return
	}
	defer rows.Close()

	columns,err := rows.Columns()
	//fmt.Println(err)
	fmt.Println(columns)

	for rows.Next(){
		rows.Scan(&id,&nickName,&age)
		fmt.Println(id,nickName,age)
	}
}

func insert(){
	result,err := db.Exec("insert into jy (nick_name,age) values(?,?)","ytt2",23)
	if err != nil{
		fmt.Println(err)
		return
	}



	id,err := result.LastInsertId()
	fmt.Println(id,err)
}
